<?php
require_once (APPLICATION_PATH . '/../library/phpthumblib/ThumbLib.inc.php');

class AppointmentController extends GTL_Action {

    public function init() {
        parent::init();
        $this->lng_id = '1';
       
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->redirector('form', 'appointment', 'default');
    }

   
    public function listAction(){
        
        
        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $orderField = 'app_id';
        $sort = 'asc';
        
        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;
        
        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }

        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
        $appointmentModel = new Model_Appointment();
        
        $appointmentData = $appointmentModel->fetchAppointmentAllData($where, $itemsPerPageReviews, $offset, $orderField, $sort);
       // echo 'in';exit;
        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($appointmentModel->getAppointmentCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'appointment', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->appointmentData = $appointmentData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;

    }


    public function formAction() {

        $successmessage = "";
        $mem_description = "";
        $errorMessageArray = Array();
        $this->view->mode = "Add";
        $data = Array();
        
        
       
        
        $appointmentModel = new Model_Appointment();
        $appointmentForm = new Form_Appointment();
        
        $memberModel = new Model_Member();
       //Load client select box
        
        $memberArray = $memberModel->fetchMemberData();
        $appointmentForm->app_member_id->addMultiOptions($memberArray);
        
        
         $appointmentData = Array();
        //get property id
        $appointmentData['prp_id'] = $this->_request->getParam('prp_id');
        
        $appointmentData['app_id'] = $this->_request->getParam('app_id');
        $app_id = (!empty($appointmentData['app_id'])) ? $appointmentData['app_id'] : null;

        

        
        if (!empty($app_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $appointmentModel->fetchEntryById($app_id);
            $appointmentData['prp_id'] = $data['prp_id'];
        }
        
         $this->view->form = $appointmentForm->populate($data);
         if ($this->getRequest()->isPost()) {
              //Check for Errors
            $postedData = $this->_request->getPost();
        
            if (!$appointmentForm->isValid($postedData)) {
                $errorMessage = $appointmentForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            }else{
                if (count($errorMessageArray) > 0) {
                }else {
                     //Write down the code to insert and add new appointment
                   
                    
                    $appointmentData['app_member_id'] = $postedData['app_member_id'];
                    $appointmentData['app_date'] = $postedData['app_date'];
                    $appointmentData['app_note'] = $postedData['app_note'];
                   
                    $appointmentModel->save($appointmentData, 'app_id');
                    if ($this->view->mode == "Add") {
                        $successmessage = 'Appointment add successfully';
                    } else {
                        $successmessage = 'Appointment updated successfully';
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'appointment', '');
                    
                }
            }
            
            
         }
        
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->mode = $this->view->mode;
        $this->view->form = $appointmentForm;
    }
  
   
   
    public function deleteAction() {


        if ($this->_getParam('app_id') && $this->_getParam('app_id') != "") {
            $appointment_id = $this->_getParam('app_id');

            $appointmentModel = new Model_Appointment();
            $appointmentModel->delete("app_id = '" . $appointment_id . "'");

            $successmessage = "Appointment deleted successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('list', 'appointment', '');
            exit;
        }
    }

     
}

   
?>
